﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectTest.Models
{
    public class QLGiaoVien
    {
        private DBTestDataContext db = new DBTestDataContext();

        //lay thong tin tat ca giao vien
        public IEnumerable<GiaoVien> GetAll()
        {
            return db.GiaoViens;
        }

        //them thong tin sinh vien
        public void Create(GiaoVien gv)
        {
            try
            {
                db.GiaoViens.InsertOnSubmit(gv);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        //Xoa thong tin xin vien theo ma
        public void Delete(string maGV)
        {
            GiaoVien gv = db.GiaoViens.SingleOrDefault(p => p.MaGV == maGV);
            try
            {
                db.GiaoViens.DeleteOnSubmit(gv);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
 
        }
        //cap nhat thong tin giao vien
        public void Edit(GiaoVien gvEdit, string maGV)
        {
            GiaoVien gv = db.GiaoViens.SingleOrDefault(p => p.MaGV == maGV);
            try
            {
                UpdateInfo(gv, gvEdit);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void UpdateInfo(GiaoVien gv,GiaoVien gvEdited)
        {
            gv.TenGV = gvEdited.TenGV;
            gv.NgaySinh = gvEdited.NgaySinh;
            gv.SDT = gvEdited.SDT;
            gv.Email = gvEdited.Email;

        }
        //tim thong tin giao vien theo ten giao vien
        public IEnumerable<GiaoVien> FindByName(string name)
        {
             return  db.GiaoViens.Where(p => p.TenGV.ToLower().Contains(name.ToLower()));
           
        }

      
    }
}